<?

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
#
#	FILE:			includes/custom/adminsections.php
#	FUNCTION:		Custom handler for the Admin Sections page
#	AUTHOR:			Cameron Morrow
#	CREATED:		27/07/2005
#
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -


# Get section names
$directory_array = getSectionNames();

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Check for added section
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if (@$_POST["newsection"]) {

	# Get section to add
	$new_section = strtolower($_POST["newsection"]);

	# Check valid chars
	if (containsValidChars($new_section, $VALID_CHARS_ID)) {

		# Check it doesn't already exist
		if (findInArray($directory_array, $new_section) === false) {

			# Attempt to create
			if (createDirectory("../", $new_section)) {

				# Confirm
				addMessage("<p>OK, the section <em>" . $new_section . "</em> was added.</p>");

				# Log
				addLog($new_section, 19);

				# Get section names again
				$directory_array = getSectionNames();

			} else {

				addMessage("<p>Sorry, an error occured and the new section could not be added. Check the Errors for details.</p>", 2);
			}
		} else {

			addMessage("<p>Sorry, a section <em>" . $new_section . "</em> already exists. Please choose a different name for the section.</p>", 3);
		}
	} else {

		addMessage("<p>Sorry, the name <em>" . $new_section . "</em> contains invalid characters. Please use only letters, numbers, and underscores.</p>", 3);
	}
}

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Check for edited sections
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if (@$_POST["sectionedited"]) {

	# Record success
	$rename_success = true;

	# Failed directories
	$failed_directories = array();

	# For each directory
	foreach ($directory_array as $key => $old_directory_name) {

		# Get new name
		$new_directory_name = @$_POST["dirname" . $key];

		# Check name is valid
		if (containsValidChars($new_directory_name, $VALID_CHARS_ID)) {

			# If it's different, rename
			if ($new_directory_name != $old_directory_name) {

				# If rename successful, update database
				if (renameDirectory("../", $old_directory_name, $new_directory_name) === true) {
					
					# Execute Update
					executeSQLQuery("UPDATE " . $PROJECT_DB_TABLES["pages"] . " SET p_section = '" . $new_directory_name . "' WHERE p_section = '" . $old_directory_name . "'");

				} else {

					# Append directory name
					$failed_directories[] = $old_directory_name;

					# Fail
					$rename_success = false;
				}
			}
		} else {

			# Add error
			addError("<p>New section name <em>" . $new_directory_name . "</em> contains invalid characters.</p>", 3);

			# Append directory name
			$failed_directories[] = $old_directory_name;

			# Fail
			$rename_success = false;
		}
	}

	# Get names again
	$directory_array = getSectionNames();

	# Feedback
	if ($rename_success) {

		addMessage("<p>Section names were updated successfully.</p>");

		# Log
		addLog(implode(",", $directory_array), 20);

	} else {

		addMessage("<p>Sorry, an error occured and the section(s) " . implode(", ", $failed_directories) . " could not be renamed. Check the Errors for details.</p>", 2);
	}
}


# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Check for deleted section
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if (@$_POST["deletesection"]) {

	# ID of section to delete
	$delete_section_id = $_POST["deletesection"];

	# Delete pages in database
	$page_delete_success = executeSQLQuery("DELETE FROM " . $PROJECT_DB_TABLES["pages"] . " WHERE p_section = '" . $delete_section_id . "'");

	# Delete directory
	if ($page_delete_success) {

		# Delete success
		if (deleteDirectory("../", $delete_section_id)) {

			# Message
			addMessage("<p>Section <em>" . $delete_section_id . "</em> was deleted successfully.</p>");

			# Log
			addLog($delete_section_id, 21);

		} else {

			# Message
			addMessage("<p>Section <em>" . $delete_section_id . "</em> could not be deleted. Check the Errors for more details.</p>", 2);
		}

	} else {

		# Message
		addMessage("<p>Sorry, the pages could not be deleted for section <em>" . $delete_section_id . "</em>. Please check the Errors for more information.</p>", 2);
	}

	# Get names again
	$directory_array = getSectionNames();
}

# Header
addContent("<h2 class=\"section\">Browsing Admin Sections</h2>\n");

# Form
addContent("<form name=\"editsectionform\" id=\"editsectionform\" action=\"" . $PAGE_ID . ".php\" method=\"post\">\n" .
	"<p>These are the current sections. Edit the text fields to change the names, or click \"Delete\" to remove the sections. <strong>Any pages within these sections will also be destroyed.</strong></p>\n" .
	"<table class=\"admintable\" cellspacing=\"0\">\n" .
	"<thead>\n" .
	"<tr>\n" .
	"<th>Section</th><th>Actions</th>\n" .
	"</tr>\n" .
	"</thead>\n" .
	"<tbody>\n");

# Content
foreach ($directory_array as $key => $directory_name) {
	addContent("<tr>\n<td><input type=\"text\" size=\"60\" name=\"dirname" . $key . "\" id=\"dirname" . $key . "\" value=\"" . $directory_name . "\" /></td>\n" .
		"<td><a href=\"javascript:deleteSection('" . $directory_name . "');\">Delete</a></td></tr>");
}

# End Form
addContent("<tr><td colspan=\"2\"><input type=\"hidden\" name=\"sectionedited\" id=\"sectionedited\" value=\"1\" /><input type=\"submit\" value=\"Save Changes\" /></td></tr></tbody></table>\n</form>\n");

# Delete form
addContent("<form name=\"deletesectionform\" id=\"deletesectionform\" action=\"" . $PAGE_ID . ".php\" method=\"post\">" .
	"<input type=\"hidden\" name=\"deletesection\" id=\"deletesection\" value=\"\" />" .
	"</form>");

# Add form
addContent("<h2 class=\"sectionadd\">Adding a section</h2>\n" .
	"<p>Enter the name of the new section below. Please use only lowercase letters, numbers, and underscores:</p>" .
	"<form name=\"addsectionform\" id=\"addsectionform\" action=\"" . $PAGE_ID . ".php\" method=\"post\">" .
	"<p><input type=\"text\" size=\"60\" name=\"newsection\" id=\"newsection\" value=\"\" /> <input type=\"submit\" value=\"Add Section\" /></p>" .
	"</form>");

?>